package com.are;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class Recibidor extends BroadcastReceiver{
	
	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction(); 
		
		if (Intent.ACTION_BOOT_COMPLETED.equals(action)){
			Intent servicio = new Intent();
		        servicio.setAction("com.are.Locator");
		        context.startService(servicio);
		}
		
		if (Intent.ACTION_USER_PRESENT.equals(action)) {
			if (!isMyServiceRunning(context)){
				Intent servicio = new Intent();
		        servicio.setAction("com.are.Locator");
		        context.startService(servicio);
			}
		}
	}
	
	private boolean isMyServiceRunning(Context context) {
		ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		for (RunningServiceInfo service : manager
				.getRunningServices(Integer.MAX_VALUE)) {
			if ("com.are.Locator".equals(service.service.getClassName())) {
				return true;
			}
		}
		return false;
	}
	
}